package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.qo.JsonResult;
import cn.wolfcode.crm.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("systemDictionaryItem")
@Controller
public class SystemDictionaryItemController {

	@Autowired
	private ISystemDictionaryItemService systemDictionaryItemService;

	@Autowired
	private ISystemDictionaryService systemDictionaryService;
	
	@RequestMapping("list")
	public String list(Model model, @ModelAttribute("qo")SystemDictionaryItemQueryObject qo){
		//int i = 1/0;
		PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.query(qo);
		model.addAttribute("pageInfo",pageInfo);
		List<SystemDictionary> systemDictionaries = systemDictionaryService.list();
		model.addAttribute("systemDictionaries",systemDictionaries);
		return "systemDictionaryItem/list";
	}
	
	/*@RequestMapping("input")
	@RequriedPermission(name="部门编辑",expression = "systemDictionaryItem:input")
	public String input(Long id,Model model){
		if (id!=null) {
			SystemDictionaryItem systemDictionaryItem = systemDictionaryItemService.select(id);
			model.addAttribute("systemDictionaryItem", systemDictionaryItem);
		}
		return "systemDictionaryItem/input";
	}*/
	
	@RequestMapping("delete")
	@ResponseBody
	public JsonResult delete(Long id){
		try {
			if (id!=null) {
				systemDictionaryItemService.delete(id);
			}
			return new JsonResult(true,"删除成功");
		}catch (Exception e){
			e.printStackTrace();
			return new JsonResult(false,"删除失败");
		}
	}
	@RequestMapping("saveOrUpdate")
	@ResponseBody
	public JsonResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem){
		try {
			if (systemDictionaryItem.getId()!=null) {
				systemDictionaryItemService.update(systemDictionaryItem);
			}else {
				systemDictionaryItemService.insert(systemDictionaryItem);
			}
			return new JsonResult(true,"新增或修改成功");
		}catch (Exception e){
			e.printStackTrace();
			return new JsonResult(false,"新增或修改失败");
		}

	}
}
